package com.mingtu.guards.activity;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.android.flexbox.AlignItems;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.mingtu.common.base.BaseApplication;
import com.mingtu.common.callback.StringDialogCallback;
import com.hjq.gson.factory.GsonFactory;
import com.mingtu.common.decoration.BottomSpaceItemDecoration;
import com.mingtu.common.decoration.TopSpaceItemDecoration;
import com.mingtu.common.utils.ActivityUtil;
import com.mingtu.common.utils.Constant;
import com.mingtu.common.utils.IntentUtils;
import com.mingtu.common.utils.MyLogUtil;
import com.mingtu.common.utils.MyUtills;
import com.mingtu.common.utils.UIUtils;
import com.mingtu.common.view.EmptyDataLayout;
import com.mingtu.hikvideo2.activity.NewPlaybackActivity;
import com.mingtu.hikvideo2.activity.NewPreviewActivity;
import com.mingtu.hikvideo2.adapter.VideoAdapter2;
import com.mingtu.hikvideo2.bean.PlaybackBean;
import com.mingtu.hikvideo2.bean.VideoListBean;
import com.mingtu.guards.R;
import com.mingtu.guards.adapter.FilteAddressAdapter;
import com.mingtu.guards.adapter.MonitorTypeAdapter;
import com.mingtu.guards.base.MyBaseActivity;
import com.mingtu.guards.bean.CameraTypeBean;
import com.mingtu.guards.bean.DeptBean;
import com.mingtu.guards.utils.MyConstant;
import com.mingtu.guards.view.FilterCancleView;
import com.mingtu.guards.view.FilterSureView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.mingtu.guards.utils.MyConstant.animationType;
import static com.mingtu.guards.utils.MyConstant.isFirstOnly;
import static com.mingtu.guards.utils.MyConstant.isOpenLoadAnimation;

public class MonitoringActivity extends MyBaseActivity {

    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.srl_up)
    SmartRefreshLayout srlUp;
    @BindView(R.id.view_flag1)
    View viewFlag1;
    @BindView(R.id.tv_text1)
    TextView tvText1;
    @BindView(R.id.layout1)
    LinearLayout layout1;
    @BindView(R.id.view_flag2)
    View viewFlag2;
    @BindView(R.id.tv_text2)
    TextView tvText2;
    @BindView(R.id.layout2)
    LinearLayout layout2;
    @BindView(R.id.checkbox_filt)
    CheckBox checkboxFilt;
    @BindView(R.id.layout_filt)
    LinearLayout layoutFilt;
    @BindView(R.id.layout_nav)
    LinearLayout layoutNav;
    @BindView(R.id.tv_end_time)
    TextView tvEndTime;
    @BindView(R.id.layout_time)
    LinearLayout layoutTime;
    @BindView(R.id.tv_fold_depart)
    TextView tvFoldDepart;
    @BindView(R.id.layout_arrow_depart)
    LinearLayout layoutArrowDepart;
    @BindView(R.id.layout_depart)
    RelativeLayout layoutDepart;
    @BindView(R.id.recycler_depart)
    RecyclerView recyclerDepart;
    @BindView(R.id.layout_right_drawer)
    LinearLayout layoutRightDrawer;
    @BindView(R.id.layout_drawer_camera)
    DrawerLayout layoutDrawerCamera;
    @BindView(R.id.tv_fold_type)
    TextView tvFoldType;
    @BindView(R.id.layout_arrow_type)
    LinearLayout layoutArrowType;
    @BindView(R.id.layout_type)
    RelativeLayout layoutType;
    @BindView(R.id.recycler_type)
    RecyclerView recyclerType;
    @BindView(R.id.filter_cancle_view)
    FilterCancleView filterCancleView;
    @BindView(R.id.filter_sure_view)
    FilterSureView filterSureView;

    private int currentPage = 1;
    private boolean isLoadMore = true;
    private VideoAdapter2 videoAdapter;
    private String appkey;
    private String deptName = null;
    private int navIndex = 0;
    private long endTimeMillis = 0;
    private String endTime = "";
    private List<String> deptList = new ArrayList<>();
    private List<String> typeList = new ArrayList<>();
    private FilteAddressAdapter filteAddressAdapter;
    private MonitorTypeAdapter monitorTypeAdapter;
    private String typeName;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_monitoring;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        super.initView();
        initListener();

        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        recycler.addItemDecoration(new TopSpaceItemDecoration());
        recycler.addItemDecoration(new BottomSpaceItemDecoration());
        recycler.setLayoutManager(layoutManager);
        setModuleTitle("视频监控");


    }

    @Override
    protected void getData() {
        getCameraList(1);
        getCameraConfigInfo();


        videoAdapter = new VideoAdapter2(context);
        if (isOpenLoadAnimation) {
            videoAdapter.isFirstOnly(isFirstOnly);
            videoAdapter.openLoadAnimation(animationType);
        }

        videoAdapter.setEmptyView(new EmptyDataLayout(context));
        recycler.setAdapter(videoAdapter);
        videoAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<VideoListBean.PageBean.ListBean> listBeans = videoAdapter.getData();
                VideoListBean.PageBean.ListBean listBean = listBeans.get(position);
                String code = listBean.getCode(); //视频的code
                String title = listBean.getName(); //视频的title
                if (StringUtils.isEmpty(appkey)) {
                    ToastUtils.showLong("appkey数据获取错误！");
                    return;
                }
                //648f27563fab4b9dbcea360d79425f82
                if (navIndex == 0) {
                    getRtspUrl(code, title);
                } else if (navIndex == 1) {
                    getRtspPlayBack(code, title);
                }
            }
        });

        FlexboxLayoutManager flm1 = new FlexboxLayoutManager(this);
        flm1.setFlexWrap(FlexWrap.WRAP);
        flm1.setFlexDirection(FlexDirection.ROW);
        flm1.setAlignItems(AlignItems.STRETCH);
        flm1.setJustifyContent(JustifyContent.FLEX_START);
        recyclerDepart.setLayoutManager(flm1);

        filteAddressAdapter = new FilteAddressAdapter(BaseApplication.getBaseApplication(), deptList);
        recyclerDepart.setAdapter(filteAddressAdapter);
        getDeptList();


        FlexboxLayoutManager flm2 = new FlexboxLayoutManager(this);
        flm2.setFlexWrap(FlexWrap.WRAP);
        flm2.setFlexDirection(FlexDirection.ROW);
        flm2.setAlignItems(AlignItems.STRETCH);
        flm2.setJustifyContent(JustifyContent.FLEX_START);
        recyclerType.setLayoutManager(flm2);

        monitorTypeAdapter = new MonitorTypeAdapter(BaseApplication.getBaseApplication(), typeList);
        recyclerType.setAdapter(monitorTypeAdapter);

        filterCancleView.setOnClickListener(new FilterCancleView.OnClickListener() {
            @Override
            public void onClickCancel() {
                deptName = null;
                typeName = null;
                endTimeMillis = 0;
                closeRightDrawers();
                filteAddressAdapter.resetData();
                monitorTypeAdapter.resetData();
            }
        });

        filterSureView.setOnClickListener(new FilterSureView.OnClickListener() {
            @Override
            public void onClickReset() {
                deptName = null;
                typeName = null;
                endTimeMillis = 0;
                closeRightDrawers();
                resetEvent();
                tvEndTime.setText("选择时间");
                filteAddressAdapter.resetData();
                monitorTypeAdapter.resetData();
            }

            @Override
            public void onClickSure() {
                if (StringUtils.isEmpty(typeName) && StringUtils.isEmpty(deptName) && navIndex == 0) { //只有视频播放的时候才有限制
                    ToastUtils.showLong("请先选择筛选条件~");
                    return;
                }
                closeRightDrawers();
                sureEvent(deptName, endTimeMillis);
            }
        });

        getCameraType();

    }


    @OnClick({R.id.layout1, R.id.layout2, R.id.layout_filt, R.id.tv_end_time})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout1:
                setNavTitle(0);
                break;
            case R.id.layout2:
                setNavTitle(1);
                break;
            case R.id.layout_filt:
                //筛选
                openRightDrawer();
                break;
            case R.id.tv_end_time:
                initTimePicker();
                break;

        }
    }

    private void setNavTitle(int index) {
        navIndex = index; //赋值nav 类型  跳转的时候用
        if (index == 0) {
            tvText1.setTextColor(getResources().getColor(R.color.colorPrimary));
            viewFlag1.setVisibility(View.VISIBLE);
            tvText1.setTypeface(Typeface.DEFAULT_BOLD);

            tvText2.setTextColor(getResources().getColor(R.color.text_color999));
            viewFlag2.setVisibility(View.INVISIBLE);
            tvText2.setTypeface(Typeface.DEFAULT);
//            ((MainActivity) getActivity()).setFilteTimeLayoutShow(false); //时间筛选的视图隐藏
//            ((MainActivity) getActivity()).resetFiltAdapteData();

        } else if (index == 1) {
            tvText2.setTextColor(getResources().getColor(R.color.colorPrimary));
            viewFlag2.setVisibility(View.VISIBLE);
            tvText2.setTypeface(Typeface.DEFAULT_BOLD);

            tvText1.setTextColor(getResources().getColor(R.color.text_color999));
            tvText1.setTypeface(Typeface.DEFAULT);
            viewFlag1.setVisibility(View.INVISIBLE);
//            ((MainActivity) getActivity()).setFilteTimeLayoutShow(true);//时间筛选的视图显示
//            ((MainActivity) getActivity()).resetFiltAdapteData();
        }

    }

    private void initListener() {
        initPullRefresh();//刷新
        initLoadMoreListener();//加载更多
    }

    private void initPullRefresh() {
        srlUp.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                srlUp.finishRefresh(Constant.REFRESH_TIME);
                srlUp.setNoMoreData(false);
                currentPage = 1;
                getCameraList(1);
            }
        });
    }

    private void initLoadMoreListener() {
        srlUp.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                if (isLoadMore) {
                    currentPage = MyUtills.loadPage(videoAdapter.getData(), Constant.PAGE_SIZE);
                    getCameraList(currentPage);
                }
                srlUp.finishLoadMore(Constant.LOADMORE_TIME);
            }
        });

    }

    private void getRtspPlayBack(String code, String title) {

        //获取系统的日期
//        Calendar currentCalendar = Calendar.getInstance();
//
//        //前一天的时间
//        Calendar yesterdayCalendar = Calendar.getInstance();
//        yesterdayCalendar.setTimeInMillis(currentCalendar.getTimeInMillis() - 24 * 60 * 60 * 1000);
//
//
//        if (startTimeMillis == 0 || endTimeMillis == 0) { //为0的时候说明未选择视频筛选的开始时间,获取当前时间前一天的视频数据
//            beginTime = TimeUtils.millis2String(yesterdayCalendar.getTimeInMillis(), "yyyy-MM-dd HH:mm:ss");
//            endTime = TimeUtils.millis2String(currentCalendar.getTimeInMillis(), "yyyy-MM-dd HH:mm:ss");
//            bTimeMillis = yesterdayCalendar.getTimeInMillis() + "";
//            eTimeMillis = currentCalendar.getTimeInMillis() + "";
//        } else {
//            beginTime = TimeUtils.millis2String(startTimeMillis, "yyyy-MM-dd HH:mm:ss");
//            endTime = TimeUtils.millis2String(endTimeMillis, "yyyy-MM-dd HH:mm:ss");
//            bTimeMillis = startTimeMillis + "";
//            eTimeMillis = endTimeMillis + "";
//        }

        if (endTimeMillis != 0) {
            endTime = TimeUtils.millis2String(endTimeMillis, "yyyy-MM-dd");
        } else {
            endTime = null;
        }

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("beginTime", endTime);
        hashMap.put("code", code);
        String str = GsonUtils.toJson(hashMap);
        OkGo.<String>post(Constant.getInstance().POST_PLAY_BACK_URL)
                .tag(this)
                .upJson(str)
                .execute(new StringDialogCallback(this) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body();//{"msg":"success","code":0,"status":"rtsp://123.7.84.20:554/openUrl/KMTa1R6?beginTime=20201226T183339&endTime=20201226T192608&playBackMode=1"}
                        if (!StringUtils.isEmpty(result)) {

                            try {
                                Gson gson = GsonFactory.getSingletonGson();
                                PlaybackBean playbackBean = new PlaybackBean();
                                playbackBean = gson.fromJson(result, PlaybackBean.class);

                                String videoUrl = playbackBean.getData().getUrl();
                                List<PlaybackBean.DataBean.ListBean> listBeans = playbackBean.getData().getList(); //视频片段列表
                                playbackBean.getData().setList(listBeans);

                                if (StringUtils.isEmpty(videoUrl)) {
                                    ToastUtils.showLong("抱歉，该设备不支持回放");
                                    return;
                                }
                                //Object implements Serializable或者继承Parcelable,list必须是ArrayList（若是List会提示错误）
                                ArrayList<Object> list = new ArrayList<Object>();
                                list.add(listBeans);
                                Bundle bundle = new Bundle();
                                bundle.putString("videoUrl", videoUrl);
                                bundle.putString("title", title + "回放");
                                bundle.putSerializable("object", list);
//                                IntentUtils.getInstance().readyGo(ActivityUtil.getTopActivity(), PlaybackActivity.class, bundle);
                                IntentUtils.getInstance().readyGo(ActivityUtil.getTopActivity(), NewPlaybackActivity.class, bundle);

                            } catch (JsonSyntaxException e) {
                                e.printStackTrace();
                            }
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        String result = response.body();
                        MyLogUtil.e(getClass().getSimpleName(), "onError==" + result);

                    }
                });
    }

    /**
     * 根据视频监控编号查询rstp地址
     */
    private void getRtspUrl(String code, String title) {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("code", code);
        String str = GsonUtils.toJson(hashMap);
        OkGo.<String>post(Constant.getInstance().POST_PREVIEW_URL)
                .tag(this)
                .upJson(str)
                .execute(new StringDialogCallback(this) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body();//{"msg":"success","code":0,"status":"rtsp://123.7.84.20:554/openUrl/9M1VWDu"}
                        if (!StringUtils.isEmpty(result)) {
                            JSONObject jsonObject = JSON.parseObject(result);
                            String msg = jsonObject.getString("msg");
                            if (msg.equals("success")) {
                                String vodeoUrl = jsonObject.getString("status");
                                if (StringUtils.isEmpty(vodeoUrl)) {
                                    ToastUtils.showLong("未获取到视频链接！");
                                    return;
                                }
                                Bundle bundle = new Bundle();
                                bundle.putString("videoUrl", vodeoUrl);
                                bundle.putString("title", title);
                                bundle.putString("code", code);
//                                IntentUtils.getInstance().readyGo(ActivityUtil.getTopActivity(), VideoPlay2Activity.class, bundle);
//                                IntentUtils.getInstance().readyGo(ActivityUtil.getTopActivity(), WebViewActivity.class, bundle);
                                IntentUtils.getInstance().readyGo(ActivityUtil.getTopActivity(), NewPreviewActivity.class, bundle);

                            }
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        String result = response.body();
                        MyLogUtil.e(getClass().getSimpleName(), "onError==" + result);

                    }
                });
    }

    /**
     * 获取摄像头配置信息
     */
    private void getCameraConfigInfo() {

        OkGo.<String>post(Constant.getInstance().POST_CAMERA_CONFIG_INFO)
                .tag(this)
                .upJson("")
                .execute(new StringDialogCallback(this) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body(); //{"msg":"success","code":0,"cameraInfo":"24571460|p765VtI8gnCXlFXAbRLJ|123.7.84.20|1443"}
                        if (!StringUtils.isEmpty(result)) {
                            JSONObject jsonObject = JSON.parseObject(result);
                            String msg = jsonObject.getString("msg");
                            if (msg.equals("success")) {
                                String cameraConfigInfo = jsonObject.getString("cameraInfo");
                                String[] info = cameraConfigInfo.split("\\|");// “.”和“|”都是转义字符，必须得加"\\";

                                appkey = info[0];
                                String secret = info[1];
                                String ip = info[2];
                                String port = info[3];

                                // HikVideoPlayerFactory.initLib(appkey, true);

                            }

                        }
                    }
                });
    }


    /**
     * 保护站下面的摄像头列表
     */
    private void getCameraList(int pageNum) {
        if (pageNum == 1) {
            currentPage = 1;
        }
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("curPage", pageNum);
        hashMap.put("deptName", deptName);
        hashMap.put("type", typeName);
        hashMap.put("pageSize", Constant.PAGE_SIZE);
        String str = GsonUtils.toJson(hashMap);
        OkGo.<String>post(Constant.getInstance().POST_CAMERA_LIST)
                .tag(this)
                .upJson(str)
                .execute(new StringDialogCallback(this) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body();
                        if (!StringUtils.isEmpty(result)) {

                            VideoListBean videoListBean = null;
                            try {
                                Gson gson = GsonFactory.getSingletonGson();
                                videoListBean = new VideoListBean();
                                videoListBean = gson.fromJson(result, VideoListBean.class);
                                List<VideoListBean.PageBean.ListBean> dataBeans = videoListBean.getPage().getList();

                                try {

                                    if (dataBeans != null) {
                                        isLoadMore = dataBeans.size() > 0 && dataBeans.size() % Constant.PAGE_SIZE == 0 ? true : false;
                                        //当是第一页的时候先清空数组，防止下拉操作的时候加载重复数据
                                        if (currentPage == 1) {
                                            videoAdapter.replaceData(dataBeans);
                                        } else {
                                            videoAdapter.addData(dataBeans);
                                        }
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                            } catch (JsonSyntaxException e) {

                                e.printStackTrace();
                                isLoadMore = false;
                                ToastUtils.showLong(getResources().getString(R.string.text_json_error));

                            }


                        }
                    }
                });
    }

    /**
     * 确定事件
     */
    public void sureEvent(String deptName, long endTimeMillis) {
        this.deptName = deptName;
        this.endTimeMillis = endTimeMillis;
        getCameraList(1);
    }


    /**
     * 重置事件
     */
    public void resetEvent() {
        getCameraList(1);
    }

    /**
     * 关闭右侧边栏
     */
    public void closeRightDrawers() {
        layoutDrawerCamera.closeDrawer(layoutRightDrawer);
    }

    /**
     * 打开右侧边栏
     */
    public void openRightDrawer() {
        layoutDrawerCamera.openDrawer(layoutRightDrawer);
    }

    public void setFilteTimeLayoutShow(boolean show) {
        if (show) {
            layoutTime.setVisibility(View.VISIBLE);
        } else {
            layoutTime.setVisibility(View.GONE);
        }
    }

    /**
     * 获得保护站名字数据
     */
    private void getDeptList() {
        OkGo.<String>post(Constant.getInstance().POST_DEPT_LIST)
                .tag(this)
                .upJson("")
                .execute(new StringDialogCallback(this) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body();
                        if (!StringUtils.isEmpty(result)) {
                            try {
                                Gson gson = GsonFactory.getSingletonGson();
                                DeptBean deptBean = new DeptBean();
                                deptBean = gson.fromJson(result, DeptBean.class);

                                List<String> deptList = deptBean.getList();
                                if (deptList != null) {
                                    if (deptList.size() > 0) {
                                        filteAddressAdapter.upData(deptList);
                                        filteAddressAdapter.setOnItemClickListener(new FilteAddressAdapter.OnItemClickListener() {
                                            @Override
                                            public void onItemClick(View view, int position) {
                                                //初始值为null,获取全部,点击筛选，选择单个保护站下的摄像头
                                                deptName = deptList.get(position);
                                                filteAddressAdapter.upData(position);
                                            }
                                        });
                                    }
                                }

                            } catch (JsonSyntaxException e) {
                                e.printStackTrace();
                            }

                        }
                    }
                });
    }

    private void initTimePicker() {
        //获取系统的日期
        Calendar selectedDate = Calendar.getInstance();
        int year = selectedDate.get(Calendar.YEAR);
        int month = selectedDate.get(Calendar.MONTH);
        int day = selectedDate.get(Calendar.DAY_OF_MONTH);
        int hour = selectedDate.get(Calendar.HOUR_OF_DAY);
        int minute = selectedDate.get(Calendar.MINUTE);
        int second = selectedDate.get(Calendar.SECOND);


//        //开始时间  一个月
//        Calendar startDate = Calendar.getInstance();
//        if (month == 0) {
//            startDate.add(Calendar.YEAR, -1);
//        } else {
//            startDate.add(Calendar.YEAR, 0);
//        }
//        startDate.add(Calendar.MONTH, -1);
//        startDate.add(Calendar.DAY_OF_MONTH, 0);
//        startDate.add(Calendar.HOUR_OF_DAY, 0);
//        startDate.add(Calendar.MINUTE, 0);
//        startDate.add(Calendar.SECOND, 0);
        Calendar startDate = Calendar.getInstance();
        startDate.setTimeInMillis(selectedDate.getTimeInMillis() - 7 * 24 * 60 * 60 * 1000);

        //结束时间
        Calendar endDate = Calendar.getInstance();


        TimePickerView pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调

//                Calendar startTime = Calendar.getInstance();
//                startTime.setTime(date);
//                int sYear = startTime.get(Calendar.YEAR);
//                int sMonth = startTime.get(Calendar.MONTH);
//                int sDay = startTime.get(Calendar.DAY_OF_MONTH);
//
//                startTime.set(sYear, sMonth, sDay, 0, 0, 0);


                endTimeMillis = TimeUtils.date2Millis(date);

                String endTime = UIUtils.getTime(date);
                tvEndTime.setText(endTime);
                tvEndTime.setTextColor(getResources().getColor(R.color.text_color333));

            }
        })
                .setType(new boolean[]{true, true, true, false, false, false})//分别对应年月日时分秒，默认全部显示
                .setContentTextSize(20)//滚轮文字大小
                .setDividerColor(Color.LTGRAY)//设置分割线的颜色
                .setCancelColor(getResources().getColor(R.color.text_cancel_color))
                .setSubmitColor(getResources().getColor(R.color.text_submit_color))
                .setTextColorCenter(getResources().getColor(R.color.colorPrimary))
                .setBgColor(Color.WHITE)
                .isDialog(true)
                .setOutSideColor(getResources().getColor(R.color.transparent))
                .setDate(selectedDate)// 如果不设置的话，默认是系统时间*/
                .setRangDate(startDate, endDate)//起始终止年月日设定
                .setLabel("年", "月", "日", "时", "分", "秒")
                .build();


        Dialog mDialog = pvTime.getDialog();
        if (mDialog != null) {

            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM);

            params.leftMargin = 0;
            params.rightMargin = 0;
            pvTime.getDialogContainerLayout().setLayoutParams(params);
            Window dialogWindow = mDialog.getWindow();
            if (dialogWindow != null) {
                dialogWindow.setWindowAnimations(com.bigkoo.pickerview.R.style.picker_view_slide_anim);//修改动画样式
                dialogWindow.setGravity(Gravity.BOTTOM);//改成Bottom,底部显示
            }
        }

        pvTime.show();
    }

    /**
     * 摄像头类型
     */
    private void getCameraType() {
        OkGo.<String>post(MyConstant.POST_CAMERA_TYPE)
                .tag(this)
                .upJson("")
                .execute(new StringDialogCallback(this) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body();
                        if (!StringUtils.isEmpty(result)) {
                            try {
                                Gson gson = GsonFactory.getSingletonGson();
                                CameraTypeBean cameraTypeBean = new CameraTypeBean();
                                cameraTypeBean = gson.fromJson(result, CameraTypeBean.class);

                                List<String> typeList = cameraTypeBean.getData();
                                if (typeList != null) {
                                    if (typeList.size() > 0) {
                                        monitorTypeAdapter.upData(typeList);
                                        monitorTypeAdapter.setOnItemClickListener(new MonitorTypeAdapter.OnItemClickListener() {
                                            @Override
                                            public void onItemClick(View view, int position) {
                                                //初始值为null,获取全部,点击筛选，选择单个保护站下的摄像头
                                                typeName = typeList.get(position);
                                                monitorTypeAdapter.upData(position);
                                            }
                                        });
                                    }
                                }

                            } catch (JsonSyntaxException e) {
                                e.printStackTrace();
                            }

                        }
                    }
                });
    }
}